#include "power_response.h"
#include "windows.h"

unsigned char uart_rx_buf[1024];
unsigned int tail;
unsigned int head;
unsigned int cmd;
unsigned int frame_count;
unsigned int frame_len;
unsigned char cmd_buf[100];
unsigned char response_buf[100];
struct power_operating_parm power_parm;

unsigned char get_uart_check(unsigned char *buf, unsigned int len)
{
    unsigned long long sum = 0;
    for (unsigned int i = 0; i < len; i++)
    {
        sum += buf[i];
    }
    return sum & 0xf;
}

typedef struct _SERIALNAMEBUF
{
    int SerialNum;
    char ppSerialName[128][10];
} SERIALNAMEBUF, *PSERIALNAMEBUF;

int EnumSerialPort(SERIALNAMEBUF *pSerialNameBuf)
{
    int i = 0;
    HKEY hKey;
    char RegKeyName[128];
    char SerialPortName[10];
    int Num = 0;

    memset(pSerialNameBuf, 0, sizeof(SERIALNAMEBUF));

    if (ERROR_SUCCESS == RegOpenKeyExA(HKEY_LOCAL_MACHINE, "Hardware\\DeviceMap\\SerialComm", NULL, KEY_READ, &hKey)) // 打开串口注册表对应的键值
    {
        DWORD dwLong, dwSize;
        while (1)
        {
            dwLong = dwSize = sizeof(RegKeyName);
            if (ERROR_NO_MORE_ITEMS == RegEnumValueA(hKey, i, RegKeyName, &dwLong, NULL, NULL, (PUCHAR)SerialPortName, &dwSize)) // 枚举串口
            {
                break;
            }
            strcpy(pSerialNameBuf->ppSerialName[Num], SerialPortName);
            Num++;
            if (Num == 128)
            {
                break;
            }
            i++;
        }
    }
    RegCloseKey(hKey);
    pSerialNameBuf->SerialNum = Num;
    return Num;
}

DWORD WINAPI power_thread(LPVOID pParam)
{
    memset(uart_rx_buf, 0, sizeof(uart_rx_buf));
    memset(response_buf, 0, sizeof(response_buf));

    tail = 0;
    head = 0;
    frame_count = 0;
    frame_len = 0;
    cmd = 0;
    while (1)
    {
        while (head != tail)
        {
            if (0 == frame_count)
            {
                if (0xFE == uart_rx_buf[head])
                {
                    cmd_buf[frame_count] = uart_rx_buf[head];
                    head++;
                    frame_count++;
                }
            }

            if (1 == frame_count)
            {
                frame_len = uart_rx_buf[head];
                cmd_buf[frame_count] = uart_rx_buf[head];
                frame_count++;
            }

            if (0 < frame_len)
            {
                if (frame_count < frame_len)
                {
                    cmd_buf[frame_count] = cmd_buf[frame_count] = uart_rx_buf[head];
                    frame_count++;
                }
                else
                {
                    if ((0xff == cmd_buf[frame_count]) && (get_uart_check(cmd_buf, frame_len) == cmd_buf[frame_count - 1]))
                    {
                        cmd = cmd_buf[2];
                        switch (cmd)
                        {
                        case 0x1: //
                        {
                            memcpy(&power_parm, &cmd_buf[3], 19);
                            ((struct uart_common_response *)response_buf)->start = 0xfe;
                            ((struct uart_common_response *)response_buf)->len = sizeof(struct uart_common_response);
                            ((struct uart_common_response *)response_buf)->cmd = 0x6;
                            ((struct uart_common_response *)response_buf)->response_info = 0x1;
                            ((struct uart_common_response *)response_buf)->check_sum = get_uart_check(response_buf, ((struct uart_common_response *)response_buf)->len - 1);
                            ((struct uart_common_response *)response_buf)->end = 0xff;
                        }

                        case 2:
                        {
                            ((struct uart_parm_query_response *)response_buf)->start = 0xfe;
                            ((struct uart_parm_query_response *)response_buf)->len = sizeof(struct uart_common_response);
                            ((struct uart_parm_query_response *)response_buf)->cmd = 0x6;
                            ((struct uart_parm_query_response *)response_buf)->power_state = power_parm.power_state;
                            ((struct uart_parm_query_response *)response_buf)->mo = power_parm.mo;
                            ((struct uart_parm_query_response *)response_buf)->fa1 = power_parm.fa1;
                            ((struct uart_parm_query_response *)response_buf)->fa2 = power_parm.fa2;
                            ((struct uart_parm_query_response *)response_buf)->fa3 = power_parm.fa3;
                            ((struct uart_parm_query_response *)response_buf)->fa4 = power_parm.fa4;
                            ((struct uart_parm_query_response *)response_buf)->fa5 = power_parm.fa5;
                            ((struct uart_parm_query_response *)response_buf)->fa6 = power_parm.fa6;
                            ((struct uart_parm_query_response *)response_buf)->fa7 = power_parm.fa7;
                            ((struct uart_parm_query_response *)response_buf)->fa8 = power_parm.fa8;

                            ((struct uart_parm_query_response *)response_buf)->output_delay = power_parm.output_delay;
                            ((struct uart_parm_query_response *)response_buf)->check_sum = get_uart_check(response_buf, ((struct uart_common_response *)response_buf)->len - 1);
                            ((struct uart_parm_query_response *)response_buf)->end = 0xff;
                        }

                        case 3:
                        {
                            ((struct uart_common_response *)response_buf)->start = 0xfe;
                            ((struct uart_common_response *)response_buf)->len = sizeof(struct uart_common_response);
                            ((struct uart_common_response *)response_buf)->cmd = 0x8;
                            ((struct uart_common_response *)response_buf)->response_info = 0x11;
                            ((struct uart_common_response *)response_buf)->check_sum = get_uart_check(response_buf, ((struct uart_common_response *)response_buf)->len - 1);
                            ((struct uart_common_response *)response_buf)->end = 0xff;
                        }

                        case 4:
                        {
                            ((struct uart_common_response *)response_buf)->start = 0xfe;
                            ((struct uart_common_response *)response_buf)->len = sizeof(struct uart_common_response);
                            ((struct uart_common_response *)response_buf)->cmd = 0x9;
                            ((struct uart_common_response *)response_buf)->response_info = 0x11;
                            ((struct uart_common_response *)response_buf)->check_sum = get_uart_check(response_buf, ((struct uart_common_response *)response_buf)->len - 1);
                            ((struct uart_common_response *)response_buf)->end = 0xff;
                        }

                        case 5:
                        {
                            ((struct uart_parm_query_response *)response_buf)->start = 0xfe;
                            ((struct uart_parm_query_response *)response_buf)->len = sizeof(struct uart_common_response);
                            ((struct uart_parm_query_response *)response_buf)->cmd = 0x6;
                            ((struct uart_parm_query_response *)response_buf)->power_state = power_parm.power_state;
                            ((struct uart_parm_query_response *)response_buf)->mo = power_parm.mo;

                            ((struct uart_parm_query_response *)response_buf)->fa1 = power_parm.fa1;
                            ((struct uart_parm_query_response *)response_buf)->fa1_state = power_parm.fa1_state;
                            ((struct uart_parm_query_response *)response_buf)->fa2 = power_parm.fa2;
                            ((struct uart_parm_query_response *)response_buf)->fa2_state = power_parm.fa2_state;
                            ((struct uart_parm_query_response *)response_buf)->fa3 = power_parm.fa3;
                            ((struct uart_parm_query_response *)response_buf)->fa3_state = power_parm.fa3_state;
                            ((struct uart_parm_query_response *)response_buf)->fa4 = power_parm.fa4;
                            ((struct uart_parm_query_response *)response_buf)->fa4_state = power_parm.fa4_state;
                            ((struct uart_parm_query_response *)response_buf)->fa5 = power_parm.fa5;
                            ((struct uart_parm_query_response *)response_buf)->fa5_state = power_parm.fa5_state;
                            ((struct uart_parm_query_response *)response_buf)->fa6 = power_parm.fa6;
                            ((struct uart_parm_query_response *)response_buf)->fa6_state = power_parm.fa6_state;
                            ((struct uart_parm_query_response *)response_buf)->fa7 = power_parm.fa7;
                            ((struct uart_parm_query_response *)response_buf)->fa7_state = power_parm.fa7_state;
                            ((struct uart_parm_query_response *)response_buf)->fa8 = power_parm.fa8;
                            ((struct uart_parm_query_response *)response_buf)->fa8_state = power_parm.fa8_state;

                            ((struct uart_parm_query_response *)response_buf)->output_delay = power_parm.output_delay;
                            ((struct uart_parm_query_response *)response_buf)->check_sum = get_uart_check(response_buf, ((struct uart_common_response *)response_buf)->len - 1);
                        }

                        case 0xa:
                        {
                            ((struct uart_common_response *)response_buf)->start = 0xfe;
                            ((struct uart_common_response *)response_buf)->len = sizeof(struct uart_common_response);
                            ((struct uart_common_response *)response_buf)->cmd = 0x9;
                            ((struct uart_common_response *)response_buf)->response_info = 0x11;
                            ((struct uart_common_response *)response_buf)->check_sum = get_uart_check(response_buf, ((struct uart_common_response *)response_buf)->len - 1);
                            ((struct uart_common_response *)response_buf)->end = 0xff;
                        }
                        }
                    }
                }
            }
        }
    }
}
